package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.database.model.BookInfo;

/**
 * 书签列表
 * 
 */
public class LabelListAdapter extends BaseAdapter {

    private List<BookInfo> list;
    private LayoutInflater inflater;

    // 构造
    public LabelListAdapter(Context context, List<BookInfo> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
    }

    public void setDataList(List<BookInfo> bookList) {
        this.list = bookList;
    }

    @Override
    public int getCount() {

        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        Holder holder = null;
        if (convertView != null) {
            view = convertView;
            holder = (Holder) view.getTag();
        } else {
            view = inflater.inflate(R.layout.item_label, parent, false);
            holder = (Holder) view.getTag();
            if (holder == null) {
                holder = new Holder();
                holder.label_title_iv = (ImageView) view.findViewById(R.id.label_title_iv);
                holder.label_bookname_text = (TextView) view.findViewById(R.id.label_bookname_text);
                holder.label_description_text =
                        (TextView) view.findViewById(R.id.label_description_text);
                holder.label_type_text = (TextView) view.findViewById(R.id.label_type_text);
                holder.label_author_text = (TextView) view.findViewById(R.id.label_author_text);
                view.setTag(holder);
            }
        }
        BookInfo info = this.list.get(position);
        if (info != null) {
            holder.label_bookname_text.setText(info.getBookName());
            holder.label_description_text.setText(info.getBookIntro());
            holder.label_type_text.setText(info.getBookType());
            holder.label_author_text.setText(info.getBookAuthorName());

            // 异步加载图片
            SqImageManager.getImageManager().displayImage(info.getBookCoverImgUrl(), holder.label_title_iv,
                    SqImageManager.IMG_DEFAULT);
        }
        return view;
    }

    private static class Holder {
        public ImageView label_title_iv;// 图片
        public TextView label_bookname_text;// 书名
        public TextView label_description_text;// 书籍简介
        public TextView label_type_text;// 书籍类型
        public TextView label_author_text;// 作者
    }

}
